package 比较器排序Comparator的使用;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeDemo {
    public static void main(String[] args) {
        TreeSet<Student> ts=new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                //s1,s2
                int num= s1.getAge()-s2.getAge();
                int num2=num==0 ? s1.getName().compareTo(s2.getName()):num;
                return num2;
            }
        });//（包装类类型)集合只能存储引用类型不能用int

        //添加元素
        Student s1=new Student("姜恒星",30);
        Student s2=new Student("姜恒",20);
        Student s3=new Student("姜",10);

        Student s4=new Student("姜",10);
        Student s5=new Student("jiang",10);
        Student s6=new Student("jiang",10);

        //添加到集合
        ts.add(s1);
        ts.add(s2);//s2存储需要比较重写方法返回值 s2>s1则升序，s2<s1则降序
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);
    }
}
